{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4fa0ce4484f8"
   },
   "source": [
    "##### Copyright 2020 Google"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "906e07f6e562"
   },
   "outputs": [],
   "source": [
    "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "def8e2071025"
   },
   "source": [
    "# Fermi-Hubbard spin-charge separation results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2b4c497af23c"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://www.example.org/cirq/experiments/fermi_hubbard/publication_results\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/ReCirq/blob/master/docs/fermi_hubbard/publication_results.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/ReCirq/blob/master/docs/fermi_hubbard/publication_results.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/ReCirq/docs/fermi_hubbard/publication_results.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5876e9883c71"
   },
   "source": [
    "This notebook presents the experimental data which was collected on Google Rainbow processor for the [Fermi-Hubbard spin-charge separation experiment](https://arxiv.org/abs/2010.07965)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "cb618a377fef"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import recirq\n",
    "except ImportError:\n",
    "    print(\"Installing ReCirq...\")\n",
    "    !pip install git+https://github.com/quantumlib/recirq --quiet\n",
    "    print(\"Installed ReCirq!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "114da845c477"
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "from recirq.fermi_hubbard import (\n",
    "    InstanceBundle,\n",
    "    apply_rescalings_to_bundles,\n",
    "    find_bundles_rescalings,\n",
    "    load_experiment,\n",
    "    plot_quantity\n",
    ")\n",
    "from recirq.fermi_hubbard.publication import (\n",
    "    parasitic_cphase_compensation\n",
    ")\n",
    "\n",
    "# Hide numpy warnings\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0f08a327887e"
   },
   "source": [
    "## Get the data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7e8bf422e2bc"
   },
   "source": [
    "In order to run this notebook, the data sets `gaussians_1u1d.zip`, `trapping_2u2d.zip` and `trapping_3u3d.zip` need to be downloaded from [https://doi.org/10.5061/dryad.crjdfn32v](https://doi.org/10.5061/dryad.crjdfn32v)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2290946dfac6"
   },
   "source": [
    "> **Note**: If you have already downloaded the data, skip the next cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "41a4067b5ec9"
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "if [ -d fermi_hubbard_data ]; then\n",
    "  echo \"Data directory found. Not downloading data.\"\n",
    "else\n",
    "  mkdir fermi_hubbard_data\n",
    "  for f in 706210 706211 706212 706213\n",
    "  do\n",
    "     wget \"https://datadryad.org/stash/downloads/file_stream/${f}\" -O fermi_hubbard_data/\"${f}.zip\" --quiet\n",
    "  done\n",
    "\n",
    "  cd fermi_hubbard_data; unzip -q \"*.zip\"; cd -\n",
    "  echo \"Fermi-Hubbard experimental data succesfully downloaded.\"\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5e62a538a6d2"
   },
   "source": [
    "Now we specify where the data is located (relative to the location of this notebook)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1f074e633290"
   },
   "source": [
    "> **Note**: If you skipped the previous codeblock, change `data_dir` in the next cell to where your data is located."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "5b67bd90ed3e"
   },
   "outputs": [],
   "source": [
    "data_dir = \"fermi_hubbard_data\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d3a498c1b85e"
   },
   "source": [
    "## Noninteracting Gaussians"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "43b329d3032d"
   },
   "outputs": [],
   "source": [
    "# Load results and create a bundle with extracted quantities.\n",
    "gaussians_1u1d_files = glob.glob(f'{data_dir}/gaussians_1u1d/0.0/*.json')\n",
    "gaussians_bundle = InstanceBundle(\n",
    "    experiments=[load_experiment(file) for file in gaussians_1u1d_files],\n",
    "    steps=range(65),\n",
    "    rescale_steps=range(65))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "20159b35e971"
   },
   "outputs": [],
   "source": [
    "# Simulate the exact numerical results that are used as a reference.\n",
    "with tqdm(range(len(gaussians_bundle.steps))) as progress:\n",
    "    def post_run(_1, _2):\n",
    "        progress.update()\n",
    "    gaussians_bundle.cache_exact_numerics(post_run_func=post_run)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "487e441dfdc4"
   },
   "outputs": [],
   "source": [
    "plot_quantity(gaussians_bundle, 'post_selection', show_std_dev=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "13a744c4dee3"
   },
   "outputs": [],
   "source": [
    "plot_quantity(gaussians_bundle, 'scaling', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bfde46ef735d"
   },
   "outputs": [],
   "source": [
    "# The data for this quantity can be viewed after double-clicking this cell output.\n",
    "plot_quantity(gaussians_bundle, 'up_down_density', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "3fcf4d8f4fe7"
   },
   "outputs": [],
   "source": [
    "plot_quantity(gaussians_bundle, 'up_down_position_average', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "cd6805d8b8b9"
   },
   "outputs": [],
   "source": [
    "plot_quantity(gaussians_bundle, 'up_down_position_average_dt', show_std_error=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bb592794a5f5"
   },
   "source": [
    "## Trapping Potential N=4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "580e9e35a603"
   },
   "outputs": [],
   "source": [
    "# Load results and create a bundles with extracted quantities for each\n",
    "# interaction strength.\n",
    "trapping_2u2d_files = [\n",
    "    glob.glob(f'{data_dir}/trapping_2u2d/{u}/*.json')\n",
    "    for u in [0.0, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]]\n",
    "trapping_2u2d_bundles = [InstanceBundle(\n",
    "    experiments=[load_experiment(file) for file in files],\n",
    "    numerics_transform=parasitic_cphase_compensation(0.138),\n",
    "    steps=range(11),\n",
    "    rescale_steps=range(11)) for files in trapping_2u2d_files]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "4ecdcd6f5275"
   },
   "outputs": [],
   "source": [
    "# Simulate the exact numerical results that are used as a reference.\n",
    "total_steps = sum(len(bundle.steps) for bundle in trapping_2u2d_bundles)\n",
    "with tqdm(range(total_steps)) as progress:\n",
    "    def post_run(_1, _2):\n",
    "        progress.update()\n",
    "    for bundle in trapping_2u2d_bundles:\n",
    "        bundle.cache_exact_numerics(post_run_func=post_run)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "f04d891f1e3a"
   },
   "outputs": [],
   "source": [
    "# Use shared rescaling values among compatible problem instances.\n",
    "apply_rescalings_to_bundles(find_bundles_rescalings(trapping_2u2d_bundles))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "e2aac2abbdb4"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_2u2d_bundles, 'post_selection', show_std_dev=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0558840ad32e"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_2u2d_bundles, 'scaling', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "a13de704fea3"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_2u2d_bundles, 'charge_spin_density', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0730d485b675"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_2u2d_bundles, 'charge_spin_spreading', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "a10c3ccc56bb"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_2u2d_bundles, 'charge_spin_spreading_dt', show_std_error=True);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3698538e1345"
   },
   "source": [
    "## Trapping Potential N=6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "653ec25655d7"
   },
   "outputs": [],
   "source": [
    "# Load results and create a bundles with extracted quantities for each \n",
    "# interaction strength.\n",
    "trapping_3u3d_files = [\n",
    "    glob.glob(f'{data_dir}/trapping_3u3d/{u}/*.json')\n",
    "    for u in [0.0, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]]\n",
    "trapping_3u3d_bundles = [InstanceBundle(\n",
    "    experiments=[load_experiment(file) for file in files],\n",
    "    numerics_transform=parasitic_cphase_compensation(0.138),\n",
    "    steps=range(11),\n",
    "    rescale_steps=range(11)) for files in trapping_3u3d_files]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0ffc7b062885"
   },
   "outputs": [],
   "source": [
    "# Simulate the exact numerical results that are used as a reference.\n",
    "total_steps = sum(len(bundle.steps) for bundle in trapping_3u3d_bundles)\n",
    "with tqdm(range(total_steps)) as progress:\n",
    "    def post_run(_1, _2):\n",
    "        progress.update()\n",
    "    for bundle in trapping_3u3d_bundles:\n",
    "        bundle.cache_exact_numerics(post_run_func=post_run)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "95d5467d355f"
   },
   "outputs": [],
   "source": [
    "# Use shared rescaling values among compatible problem instances.\n",
    "apply_rescalings_to_bundles(find_bundles_rescalings(trapping_3u3d_bundles))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "239812863961"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_3u3d_bundles, 'post_selection', show_std_dev=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "4577c5ef621d"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_3u3d_bundles, 'scaling', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "05f8980d563a"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_3u3d_bundles, 'charge_spin_density', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "f397b27ea99e"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_3u3d_bundles, 'charge_spin_spreading', show_std_error=True);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bcf9154a1afc"
   },
   "outputs": [],
   "source": [
    "plot_quantity(trapping_3u3d_bundles, 'charge_spin_spreading_dt', show_std_error=True);"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "publication_results.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
